package com.meizhitech.mzcmsapi.api;

import cloud.tianai.captcha.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.meizhitech.mzcmsapi.api.param.UserLoginParam;
import com.meizhitech.mzcmsapi.api.param.CaptchaParam;
import com.meizhitech.mzcmsapi.api.param.RegisterParam;
import com.meizhitech.mzcmsapi.utils.*;
import com.meizhitech.mzcmsapi.common.ServiceResultEnum;
import cloud.tianai.captcha.spring.vo.CaptchaResponse;
import cloud.tianai.captcha.spring.vo.ImageCaptchaVO;
import com.meizhitech.mzcmsapi.service.UserService;

import javax.validation.Valid;

@RestController
@Api(value = "v1", tags = "用户操作相关接口")
@RequestMapping("/api/v1")
public class User {

    @Autowired
    private CaptchaUtil captchaUtil;

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(User.class);

    @PostMapping("/user/captcha")
    @ApiOperation(value = "获取验证码", notes="返回base64")
    @CrossOrigin(origins = "*")
    public CaptchaResponse<ImageCaptchaVO> captcha(){
        return captchaUtil.createCaptcha();
    }

    @PostMapping("/user/checkCaptcha")
    @ApiOperation(value = "检查验证码", notes = "返回结果")
    @CrossOrigin(origins = "*")
    public ApiResponse<?> checkCaptcha(@RequestBody CaptchaParam captchaParam){
        ApiResponse<?> match = captchaUtil.checkCaptcha(captchaParam);
        System.out.println(match);
        return match;
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "注册用户",notes = "返回结果")
    public Result register(@RequestBody @Valid RegisterParam registerParam){
        //判断账号是否符合要求
        if (!NumberUtil.isPhone(registerParam.getAccount())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        //判断密码是否符合要求
        if (!StringUtil.isPassWord(registerParam.getPassword())){
            return  ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_IS_NOT_PASSWORD.getResult());
        }
        String registerResult = userService.register(registerParam);
        logger.info("register api,registerResult={}", registerResult);
        //注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
            return ResultGenerator.genSuccessResult(registerResult);
        }
        return ResultGenerator.genFailResult(registerResult);
    }

    @PostMapping("/user/login")
    @ApiOperation(value = "登录接口", notes="返回token")
    public Result login(@RequestBody @Valid UserLoginParam userLoginParam){
        //校验验证码是否正确

        //判断账号是否符合要求
        if (!NumberUtil.isPhone(userLoginParam.getAccount())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        //判断密码是否符合要求
        if (!StringUtil.isPassWord(userLoginParam.getPassword())){
            return  ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_IS_NOT_PASSWORD.getResult());
        }
        //校验账号密码是否正确
        String loginResult = userService.login(userLoginParam);
        System.out.println(loginResult);
//        String loginResult = "业务还没有实现";
        return ResultGenerator.genFailResult(loginResult);
    }
}
